﻿using NUnit.Framework;
using Ninject;
using Ninject.Modules;
using System.Net.DnsToolkit.Resolver;
using System.Net.DnsToolkit.Configuration;
namespace System.Net.DnsToolkit.TestUnit.Resolver {

    [TestFixture]
    public class ServerResolverTest {


        class WmiServerResolverModule : NinjectModule {
            public override void Load() {

                Bind<IResolver>().To<ServerResolver>();
                Bind<IDnsSocket>().To<AsyncDnsSocket>();
                Bind<IQueryBuilder>().To<INAQueryBuilder>();
                Bind<IHeaderIdGenerator>().To<RandomHeaderId>().InSingletonScope();
                Bind<IAnswerParser>().To<FirstAnswerParser>();
                Bind<IDnsServerProvider>().To<WmiDnsServerProvider>();


            }
        }

        [Test]
        public void TestResolv() {

            var modules = new NinjectModule[] {
                new WmiServerResolverModule(),
                new DnsReaderStreamModule(),
                new DnsWriterStreamModule(),
                new DnsWriterJsonModule()
            };

            using (var kernel = new StandardKernel(modules)) {
                var resolver = kernel.Get<IResolver>();

                IPAddress address = resolver.Resolv("darkdns.codeplex.com");
                Assert.AreEqual("65.55.11.218", address.ToString(), "address");
            }

        }

        class StaticServerResolverModule : NinjectModule {
            public override void Load() {

                Bind<IResolver>().To<ServerResolver>();
                Bind<IDnsSocket>().To<AsyncDnsSocket>();
                Bind<IQueryBuilder>().To<INAQueryBuilder>();
                Bind<IHeaderIdGenerator>().To<L33tHeaderId>().InSingletonScope();
                Bind<IAnswerParser>().To<FirstAnswerParser>();
                Bind<IDnsServerProvider>().To<L33tDnsServerProvider>();


            }
        }

        [Test]
        public void TestNoServer() {

            var modules = new NinjectModule[] {
                new StaticServerResolverModule(),
                new DnsReaderStreamModule(),
                new DnsWriterStreamModule(),
                new DnsWriterJsonModule()
            };

            using (var kernel = new StandardKernel(modules)) {
                var resolver = kernel.Get<IResolver>();

                IPAddress address = resolver.Resolv("darkdns.codeplex.com");
                Assert.IsNull(address, "address");
            }
        }


    }
}
